package ods_industry_2024.ods_02.indicator_count_hudi.indicator_02

import org.apache.hudi.DataSourceWriteOptions.{PARTITIONPATH_FIELD, PRECOMBINE_FIELD, RECORDKEY_FIELD}
import org.apache.hudi.QuickstartUtils.getQuickstartWriteConfigs
import org.apache.spark.sql.SparkSession

object test_03 {
  def main(args: Array[String]): Unit = {

    /*
    根据dwd_ds_hudi库中的表统计每个省每月下单的数量和下单的总金额，并按照year，month，region_id进行分组,按照total_amount逆序排序，形
    成sequence值，将计算结果存入Hudi的dws_ds_hudi数据库province_consumption_day_aggr表中（表结构如下），然后使用spark-shell根据订单
    总数、订单总金额、省份表主键均为降序排序，查询出前5条，在查询时对于订单总金额字段将其转为bigint类型（避免用科学计数法展示），将SQL语句复制粘贴
    至客户端桌面【Release\任务B提交结果.docx】中对应的任务序号下，将执行结果截图粘贴至客户端桌面【Release\任务B提交结果.docx】中对应的任务序号下
     */
    val spark=SparkSession.builder()
      .master("local[*]")
      .appName("第二套卷子指标第三题")
      .config("hive.exec.dynamic.partition.mode","nonstrict")
      .config("spark.serializer","org.apache.spark.serializer.KryoSerializer")
      .config("spark.sql.extensions","org.apache.spark.sql.hudi.HoodieSparkSessionExtension")
      .enableHiveSupport()
      .getOrCreate()


    spark.table("dwd_ds_hudi_02.fact_order_info").createOrReplaceTempView("temp01")
    spark.table("dwd_ds_hudi_02.fact_order_info")
      .where("etl_date=(select max(etl_date) from temp01)")
      .distinct()
      .createOrReplaceTempView("order_info")

    spark.table("dwd_ds_hudi_02.dim_region").createOrReplaceTempView("temp02")
    spark.table("dwd_ds_hudi_02.dim_region")
      .where("etl_date=(select max(etl_date) from temp02)")
      .createOrReplaceTempView("region")

    spark.table("dwd_ds_hudi_02.dim_province").createOrReplaceTempView("temp03")
    spark.table("dwd_ds_hudi_02.dim_province")
      .where("etl_date=(select max(etl_date) from temp03)")
      .createOrReplaceTempView("province")


    val result=spark.sql(
      """
        |select distinct
        |uuid() as uuid,
        |province_id,province_name,region_id,region_name,total_amount,total_count,
        |row_number() over(partition by year,month,region_id order by total_amount desc) as sequence,
        |year,
        |month
        |from(
        |select distinct
        |o.province_id ,
        |p.name as province_name,
        |r.id as region_id,
        |r.region_name,
        |sum(final_total_amount) over(partition by Year(to_date(o.create_time,"yyyyMMdd")),Month(to_date(o.create_time,"yyyyMMdd")),o.province_id ) as total_amount,
        |count(*) over(partition by Year(to_date(o.create_time,"yyyyMMdd")),Month(to_date(o.create_time,"yyyyMMdd")),o.province_id  ) as total_count,
        |Year(to_date(o.create_time,"yyyyMMdd")) as year,
        |Month(to_date(o.create_time,"yyyyMMdd")) as month
        |from order_info as o
        |join province as p
        |on p.id=o.province_id
        |join region  as r
        |on r.id=p.region_id
        |) as r1
        |""".stripMargin)


    spark.sql("use hudi_indicator")
    spark.sql("drop table if exists province_consumption_day_aggr_02")
    spark.sql(
      """
        |create table if not exists province_consumption_day_aggr_02(
        |uuid string,
        |province_id int,
        |province_name string,
        |region_id int,
        |region_name string,
        |total_amount double,
        |total_count int,
        |sequence int
        |)using hudi
        |tblproperties(
        |type="cow",
        |primarykey="uuid",
        |preCombineField="total_count",
        |hoodie.datasource.hive_aync.mode="hms"
        |)
        |partitioned by(year int,month int)
        |""".stripMargin)

    result.write.mode("append")
      .format("hudi")
      .options(getQuickstartWriteConfigs)
      .option(RECORDKEY_FIELD.key(),"uuid")
      .option(PRECOMBINE_FIELD.key(),"total_count")
      .option(PARTITIONPATH_FIELD.key(),"year,month")
      .option("hoodie.table.name","province_consumption_day_aggr_02")
      .save("hdfs://192.168.40.110:9000/user/hive/warehouse/hudi_indicator.db/province_consumption_day_aggr_02")

    println("完成")



    spark.close()
  }

}
